how to create and use slice in javascript methods